<?
class ETreeSelect extends CWidget {
	
	var $assets;
	var $options;
	var $model;
	var $name;
	
	public function init(){
		$this->assets = Yii::app()->assetManager->publish(Yii::getPathOfAlias('ext.treeselect.assets'));
		
		$cs = Yii::app()->getClientScript();
		$cs->registerCoreScript('jquery');
		
		#$cs->registerCssFile($this->assets.'/css/thickbox.css' );
		$cs->registerCssFile($this->assets.'/css/ETreeSelect.css' );
		#$cs->registerScriptFile($this->assets.'/js/thickbox.js', CClientScript::POS_BEGIN);
		#$cs->registerScript(__CLASS__,'var tb_pathToImage = "'.$this->assets.'/images/loadingAnimation.gif";', CClientScript::POS_HEAD);
	}
	
	public function run(){
		$tree = $this->getTree();
		# заплатка 
		$tree = $tree[0]['children'];
		# 
		
		$this->render(__CLASS__, array(
			'tree'=>$tree,
			'id'=>$this->id,
			'model'=>$this->model,
			'name'=>$this->name,
			'options'=>$this->options,
			'uniqid'=>uniqid(),
		));
	}
	
	private function getTree(){
		if($root = Part::model()->find('alias="carclub"')) {
			$tree = $this->convertMenuTree($root->getNestedTree());
		}

		return $tree;
	}
	
	private function convertMenuTree($tree, $tmp = array()){

		foreach($tree as $node){
			$c = count($tmp);
			$tmp[$c]['text'] = $this->getText($node['node']);
			$tmp[$c]['id'] = $node['node']->id;

			if(isset($node['children']))
				$tmp[$c]['children'] = $this->convertMenuTree($node['children']);
		}
		
		return $tmp;
	}
	
	private function getText($node){
		if($node->alias == 'carclub')
			return $node->title;

		$text = CHtml::link(
				$node->title, 
				'#', 
				array('rel'=>$node->id, 'class'=>'href-like-text part-selector', 'title'=>'Выбрать раздел '.$node->title)
			);
		return $text;
	}
	
}
?>